<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>

</head>
<body>

    <form class="layui-form" lay-filter="updatePasswordFilter">
        <br>
        <div class="demo-reg-container">
            <div class="layui-form-item">
                <input type="hidden" name="id" id="id">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-inline">
                    <div class="layui-input-wrap">
                        <input type="text" name="account" id="account" value="" lay-verify="required" placeholder="账号" autocomplete="off" class="layui-input" lay-affix="clear" disabled>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-inline">
                    <div class="layui-input-wrap">
                        <input type="password" name="password" value="" lay-verify="required|Password" placeholder="原密码" autocomplete="off" class="layui-input" id="password" lay-affix="eye" onblur="checkPassword()">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <div class="layui-input-wrap">
                        <input type="password" name="newPassword" value="" lay-verify="required|newPassword" placeholder="新密码" autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye" onblur="checkNewPassword()">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-inline">
                    <div class="layui-input-wrap">
                        <input type="password" name="confirmPassword" id="confirmPassword" value="" lay-verify="required|confirmPassword" placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye" onblur="checkConfirmPassword()">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="submitUpdate">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>

    <script>

        layui.use(function(){
            var $ = layui.$;
            var form = layui.form;

            // 自定义验证规则
            form.verify({
                // 确认密码
                confirmPassword: function(value, item){
                    var passwordValue = $('#reg-password').val();
                    if(value !== passwordValue){
                        return '两次密码输入不一致';
                    }
                    if (!/^[\S]{6,16}$/.test(value)) {
                        return '密码必须为 6 到 16 位的非空字符';
                    }
                },
                // 验证密码，且为必填项
                newPassword: function(value, elem) {
                    if (!/^[\S]{6,16}$/.test(value)) {
                        return '密码必须为 6 到 16 位的非空字符';
                    }
                },
                // 原密码与新密码不能一致
                Password: function(value, item){
                    var newPasswordValue = $('#reg-password').val();
                    if(value === newPasswordValue){
                        return '原密码与新密码不能一致';
                    }
                    if (!/^[\S]{6,16}$/.test(value)) {
                        return '密码必须为 6 到 16 位的非空字符';
                    }
                }
            });



            $.post(
                '/user/selectByInfoId',
                function (result) {
                    if (result.code == 0) {
                        $('#id').val(result.data.id);
                        $('#account').val(result.data.account);
                    }
                },
                'json'
            );
            form.on('submit(submitUpdate)',function (data) {
                var field = data.field;
                console.log(field);
                $.post(
                    '/user/updatePassword',
                    field,
                    function (result) {
                        console.log(result);
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                            setInterval(function() {
                                //关闭弹出框
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            }, 2000)
                        } else {
                            mylayer.errorMsg(result.msg);
                        }
                    },
                    'json'
                );
                return false;
            });
        });
    </script>
</body>
</html>